package uk.ac.cam.echo.security;

import java.io.IOException;
import java.net.UnknownHostException;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.nfc.NfcAdapter;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.view.View;
import android.view.View.OnClickListener;

import uk.ac.cam.echo.messages.CheckInMessage;
import uk.ac.cam.echo.messages.SecurityMessage;

/**
 * @author Matt Thomson
 */

public class Scanner extends Activity {
    
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.security);
        
        //reference XML features
        String[] data = {"ID","Name","N/A","N/A","N/A"};
        final TextView tvName = (TextView) findViewById(R.id.name);
        final TextView tvRFID = (TextView) findViewById(R.id.rfid);
        final TextView tvLoc1 = (TextView) findViewById(R.id.loc1);
        final TextView tvLoc2 = (TextView) findViewById(R.id.loc2);
        final TextView tvLoc3 = (TextView) findViewById(R.id.loc3);
        final TextView tvTime1 = (TextView) findViewById(R.id.time1);
        final TextView tvTime2 = (TextView) findViewById(R.id.time2);
        final TextView tvTime3 = (TextView) findViewById(R.id.time3);
        final ImageView iv = (ImageView) findViewById(R.id.authorise);
        
        //online database information
        new DBSocket().execute();
        
        //the back button
        final Scanner thisClass = this;
        Button backButton = (Button) findViewById(R.id.back);
        backButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                Intent i = new Intent(thisClass,SecurityAppActivity.class);
                thisClass.startActivity(i);
                thisClass.finish();
            }
        });
        
        //get RFID tag
        Intent intent = getIntent();
        String hex = scan(intent);
        data[0] = hex;
        
        //get user data from database
        try {
            SecurityMessage sm = DBSecurity.getInstance().getSecurityDetails(hex);
            data[1] = sm.getForename()+" "+sm.getSurname();
            System.out.println("1");
            //WORKER DATA
            if (sm.getUserType().equals("Worker")) {
                List<CheckInMessage> list = sm.getCheckIns();
                System.out.println("2");
                int checkInNumber = list.size();
                for (int i=0; i<checkInNumber; i++) {
                    CheckInMessage c = list.get(i);
                    String checkInID = c.getAttractionName();
                    if (checkInID.equals("Worker1")) {
                        data[2] = c.getFormattedCheckInTime();
                    } else if (checkInID.equals("Worker2")) {
                        data[3] = c.getFormattedCheckInTime();
                    } else if (checkInID.equals("Worker3")) {
                        data[4] = c.getFormattedCheckInTime();
                    }
                }
                if (data[4].equals("N/A")) { //worker is still working
                    iv.setImageResource(R.drawable.authorised);
                }
                else {
                    iv.setImageResource(R.drawable.unauthorised);
                }
                tvLoc1.setText("Shift Start");
                tvLoc2.setText("Checkpoint");
                tvLoc3.setText("Shift End");
                tvTime1.setText(data[2]);
                tvTime2.setText(data[3]);
                tvTime3.setText(data[4]);
            }
            //GUEST DATA
            else {
                tvTime1.setText("GUEST");
                tvTime2.setText(" ");
                tvTime3.setText(" ");
                tvLoc1.setText(" ");
                tvLoc2.setText(" ");
                tvLoc3.setText(" ");
                iv.setImageResource(R.drawable.authorised);
            }
            tvName.setText(data[1]);
            tvRFID.setText(data[0]);
        }
        catch (UnknownHostException e) {
            tvName.setText("Unable to access database, contact technical support");
            tvRFID.setText(" ");
            tvTime1.setText(" ");
            tvTime2.setText(" ");
            tvTime3.setText(" ");
            tvLoc1.setText(" ");
            tvLoc2.setText(" ");
            tvLoc3.setText(" ");
            e.printStackTrace();
        }
        catch (IOException e) {
            tvName.setText("Unable to access database, contact technical support");
            tvRFID.setText(" ");
            tvTime1.setText(" ");
            tvTime2.setText(" ");
            tvTime3.setText(" ");
            tvLoc1.setText(" ");
            tvLoc2.setText(" ");
            tvLoc3.setText(" ");
            e.printStackTrace();
        }
        catch (InterruptedException e) {
            tvName.setText("Unable to access database, contact technical support");
            tvRFID.setText(" ");
            tvTime1.setText(" ");
            tvTime2.setText(" ");
            tvTime3.setText(" ");
            tvLoc1.setText(" ");
            tvLoc2.setText(" ");
            tvLoc3.setText(" ");
            e.printStackTrace();
        }
        catch (Exception e) {
            tvName.setText("An error has occurred, hand in phone to technical support");
            tvRFID.setText(" ");
            tvTime1.setText(" ");
            tvTime2.setText(" ");
            tvTime3.setText(" ");
            tvLoc1.setText(" ");
            tvLoc2.setText(" ");
            tvLoc3.setText(" ");
            e.printStackTrace();
        }
    }
    
    //get RFID hex string from tag
    private String scan(Intent intent) {
        byte[] data = intent.getByteArrayExtra(NfcAdapter.EXTRA_ID);
        StringBuffer hex = new StringBuffer();
        String zeroTest;
        for (int i=data.length-1; i>=0; i--) {
            zeroTest = Integer.toHexString(0xFF & data[i]); 
            if (zeroTest.length() == 1) {  
                hex.append('0'); 
            } 
            hex.append(zeroTest.toUpperCase());
        }
        return hex.toString();
    }
    
}